package com.horoscope.astrology.zodiac.signs.free.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RatingBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.NativeADViewHolder
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.ShareViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.ContentBean
import com.horoscope.astrology.zodiac.signs.free.bean.NativeAdBean
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADNativeManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class HoroscopeAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataList = ArrayList<ContentBean>()
    private var mPosition = 0
    private var mTitle = ""
    private var filedNum = 0

    fun setData(list: ArrayList<ContentBean>) {
        mDataList.clear()
        mDataList.addAll(list)
        notifyDataSetChanged()

    }

    fun setPosition(position: Int) {
        mPosition = position
    }

    fun setTitleType(type: String) {
        mTitle = type
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        when (p1) {
            TYPE_SHARE -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_share, parent, false)
                return ShareViewHolder(view)
            }
            AD -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.ad_layout, parent, false)
                return NativeADViewHolder(mContext, view)
            }
            else -> {
                val view =
                    LayoutInflater.from(mContext).inflate(R.layout.item_comtent, parent, false)
                return ContentViewHolder(view)
            }
        }

    }

    override fun getItemCount(): Int {
        return if (mDataList.size == 0) {
            0
        } else {
            mDataList.size + 1
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when {
            position == mDataList.size -> {
                TYPE_SHARE
            }
            mDataList[position].ad -> {
                AD
            }
            else -> {
                TYPE_CONTENT
            }
        }

    }

    companion object {
        const val TYPE_SHARE = 0x11
        const val TYPE_CONTENT = 0x12
        private const val AD = 0x14
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, p1: Int) {

        when (holder) {
            is ContentViewHolder -> {
                val bean = mDataList[p1]
                when (bean.title) {
                    "Love" -> {
                        holder.title.background = mContext.getDrawable(R.drawable.shape_love)
                        holder.ratingBar.setProgressDrawableTiled(mContext.getDrawable(R.drawable.rating_layer_love))

                    }
                    "Health" -> {
                        holder.title.background = mContext.getDrawable(R.drawable.shape_health)
                        holder.ratingBar.setProgressDrawableTiled(mContext.getDrawable(R.drawable.rating_layer_health))


                    }
                    "Career" -> {
                        holder.title.background = mContext.getDrawable(R.drawable.shape_career)
                        holder.ratingBar.setProgressDrawableTiled(mContext.getDrawable(R.drawable.rating_layer_career))
                    }

                }
                holder.title.text = bean.title
                holder.context.text = bean.content
                holder.ratingBar.rating = bean.rate.toFloat()
                if (mTitle != "Today" && mTitle != "Tomorrow") {
                    holder.title.visibility = View.GONE
                    holder.ratingBar.visibility = View.GONE
                    val lp = holder.context.layoutParams as ConstraintLayout.LayoutParams
                    lp.topMargin = 0
                    holder.context.layoutParams = lp
                    holder.root.setPadding(10, 0, 10, 10)
                }
            }
            is ShareViewHolder -> {
                holder.facebook.setOnClickListener {
                    ShareManager.shareToFacebook(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.more.setOnClickListener {
                    ShareManager.sysCallsShare(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
            }
            is NativeADViewHolder -> {
                val bean = mDataList[p1]
                if (bean.nativeAdBean == null) {
                    val nativeAdBean = ADNativeManager.mInstance.getNativeAdBean("Native_$mTitle")
                    if (nativeAdBean != null) {
                        mDataList[p1].nativeAdBean = nativeAdBean
                        handleNativeAd(nativeAdBean, holder, "Native_$mTitle")
                    } else if (filedNum == 0) {
                        // 协程延迟一秒
                        GlobalScope.launch(Dispatchers.Main) {
                            delay(2000L)
                            notifyItemChanged(p1)
                        }
                        filedNum = 1
                    }
                } else {
                    handleNativeAd(bean.nativeAdBean, holder, "")
                }

            }
        }
    }

    private fun handleNativeAd(
        nativeAdBean: NativeAdBean?,
        holder: NativeADViewHolder,
        placement: String
    ) {
        if (nativeAdBean != null) {
            holder.changeAD(nativeAdBean.adInfo.advertiser) // 通过advertiser判断显示那个原生广告视图
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    if (nativeAdBean.facebookNativeAD != null) {
                        holder.inflateFacebookAd(
                            nativeAdBean.facebookNativeAD,
                            "Horoscope_Small",
                            placement
                        )
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
                ADConfig.ADMOB -> {
                    if (nativeAdBean.googleNativeAd != null) {
                        holder.inflateAdMobAd(nativeAdBean.googleNativeAd!!, "Horoscope_Small")
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
            }
        } else {//没有拿到广告，从缓存中取
            holder.inVisibility()
        }
    }

    inner class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root: ConstraintLayout = itemView.findViewById(R.id.cl_root)
        val context: TextView = itemView.findViewById(R.id.tv_content)
        val title: TextView = itemView.findViewById(R.id.tv_title)
        val ratingBar: RatingBar = itemView.findViewById(R.id.rating_bar)
    }
}